package com.example.app2024.navigationButtom;

import androidx.lifecycle.ViewModelProvider;

import android.animation.ObjectAnimator;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.app2024.R;

import java.util.Random;

public class ThreeFragment extends Fragment
{

      private ThreeViewModel mViewModel;
      private ImageView imageView;

      public static ThreeFragment newInstance()
      {
            return new ThreeFragment();
      }

      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
      {
            View view = inflater.inflate(R.layout.fragment_three, container, false);
            imageView = view.findViewById(R.id.imageView);
            return  view;
      }

      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState)
      {
            super.onActivityCreated(savedInstanceState);
            mViewModel = new ViewModelProvider(this).get(ThreeViewModel.class);
            imageView.setX( imageView.getX() + mViewModel.dx);
            // TODO: Use the ViewModel
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "X", 0);
            objectAnimator.setDuration(500);
            imageView.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        if (!objectAnimator.isRunning())
                        {
                              float dx = new Random().nextBoolean() ? 100  : -100;
                              objectAnimator.setFloatValues(imageView.getX(), imageView.getX()+ dx);
                              mViewModel.dx += dx;
                              objectAnimator.start();
                        }
                  }
            });
      }

}